package com.portal.gui.marketmanger;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Feedback;
import com.portal.logic.FeedbackDAO;
import com.portal.logic.Product;
import com.portal.logic.ProductDAO;

public class GetFeedBackReportByProductScr extends JFrame {

	private JPanel contentPane;

	private static String selectedProduct;

	DefaultListModel model;
	private JTable table;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GetFeedBackReportByProductScr frame = new GetFeedBackReportByProductScr(selectedProduct);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GetFeedBackReportByProductScr(String selectedProduct) {
		setTitle("Feeddback Report by Product");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);


		ProductDAO productDAO = new ProductDAO(ConnectionManager.getConnection());
		Product product = productDAO.selectByName(selectedProduct);

		FeedbackDAO feedbackDAO = new FeedbackDAO(ConnectionManager.getConnection()) ;
		List<Feedback> feedbacks = feedbackDAO.selectFeedbacksByProductId(product.getId());

		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		btnBack.setBounds(99, 203, 97, 25);
		contentPane.add(btnBack);
		
		
		final String[] values = new String[feedbacks.size()];
		for (int i = 0; i < feedbacks.size(); i++) {
			values[i] = feedbacks.get(i).getFeedBack();
			//i++;
		}
		
		JList list = new JList();
		list.setModel(new AbstractListModel() {
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		list.setBounds(134, 23, 144, 65);
		contentPane.add(list);
	}
}
